package com.ysqcould.message.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.ysqcould.common.utils.DateUtils;
import com.ysqcould.message.domain.MemberAlbum;
import com.ysqcould.message.domain.bo.MemberAlbumBo;
import com.ysqcould.message.mapper.MemberAlbumMapper;
import com.ysqcould.message.service.IMemberAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员相册Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-30
 */
@Service
public class MemberAlbumServiceImpl implements IMemberAlbumService
{
    @Autowired
    private MemberAlbumMapper memberAlbumMapper;

    /**
     * 查询会员相册
     * 
     * @param id 会员相册主键
     * @return 会员相册
     */
    @Override
    public MemberAlbum selectMemberAlbumById(Long id)
    {

        MemberAlbum album = memberAlbumMapper.selectMemberAlbumById(id);
        String url = album.getPcUrl();
        String[] split = url.split(";");
        album.setPcUrlList(split);
        return album;
    }

    /**
     * 查询会员相册列表
     * 
     * @param memberAlbum 会员相册
     * @return 会员相册
     */
    @Override
    public List<MemberAlbum> selectMemberAlbumList(MemberAlbum memberAlbum)
    {
        List<MemberAlbum> memberAlbums = memberAlbumMapper.selectMemberAlbumList(memberAlbum);
        memberAlbums.forEach(p->{
            String[] split = p.getPcUrl().split(";");
            p.setPcUrlList(split);
        });
        return memberAlbums;
    }

    /**
     * 新增会员相册
     * 
     * @param memberAlbum 会员相册
     * @return 结果
     */
    @Override
    public int insertMemberAlbum(MemberAlbum memberAlbum)
    {
        List<String>str=new ArrayList<>();
        memberAlbum.setCreateTime(DateUtils.getNowDate());
        String[] urlList = memberAlbum.getImgUrlList();
        for (String s : urlList) {
            str.add(s);
        }
        memberAlbum.setPcUrl(str.stream().collect(Collectors.joining(";")));
        return memberAlbumMapper.insertMemberAlbum(memberAlbum);
    }

    /**
     * 修改会员相册
     * 
     * @param memberAlbum 会员相册
     * @return 结果
     */
    @Override
    public int updateMemberAlbum(MemberAlbum memberAlbum)
    {
        MemberAlbum album = memberAlbumMapper.selectMemberAlbumById(memberAlbum.getId());
        List<String>urList=new ArrayList<>();
        album.setUpdateTime(DateUtils.getNowDate());
        String[] list = memberAlbum.getImgUrlList();
        for (String s : list) {
            urList.add(s);
        }
        String collect = urList.stream().collect(Collectors.joining(";"));
        if (ObjectUtil.isEmpty(album.getPcUrl())){
            album.setPcUrl(collect);
        }else {
            album.setPcUrl(album.getPcUrl()+";"+collect);
        }
        return memberAlbumMapper.updateMemberAlbum(album);
    }

    /**
     * 批量删除会员相册
     * 
     * @param ids 需要删除的会员相册主键
     * @return 结果
     */
    @Override
    public int deleteMemberAlbumByIds(Long[] ids)
    {
        return memberAlbumMapper.deleteMemberAlbumByIds(ids);
    }

    /**
     * 删除会员相册信息
     * 
     * @param id 会员相册主键
     * @return 结果
     */
    @Override
    public int deleteMemberAlbumById(Long id)
    {
        return memberAlbumMapper.deleteMemberAlbumById(id);
    }

    @Override
    public int delImage(MemberAlbumBo bo) {
        MemberAlbum memberAlbum = memberAlbumMapper.selectByData(bo.getData());
        String pcUrl = memberAlbum.getPcUrl();
        if (pcUrl.contains(";")){
            String replace = pcUrl.replace(bo.getVal()+";", "");
            memberAlbum.setPcUrl(replace);
        }else {
            String replace = pcUrl.replace(bo.getVal(), "");
            memberAlbum.setPcUrl(replace);
        }
        return memberAlbumMapper.updateMemberAlbum(memberAlbum);
    }
}
